$(function(){
    win_w=$(window).width(); //浏览器当前窗口可视区域宽度
    win_h=$(window).height(); //浏览器当前窗口可视区域高度
	//第四批iPhone图片切换
	
    /*第一屏动画效果开始*/
    setTimeout(function() {
        $('#ev_01').find(".down_line").animate({opacity: 1,'left':'230px'},1000);
    },0);
    setTimeout(function() {
        $('#ev_01').find(".up_line").animate({opacity: 1,'left':'615px'},1000);
    },0);
    

    /*第一屏动画效果结束*/
    if(win_h>=550){
        /*滚动整屏效果开始*/
        var $scrollElem = $('html, body'),
            isIE6 = !-[1,] && !window.XMLHttpRequest,
            isMac = window.navigator.platform.toLowerCase().indexOf('mac') > -1;

        var PageCtrl = function(options) {
            this.init.call(this, options);
        };

        PageCtrl.prototype = {
            init: function(options) {
                this.curIndex = 0;
                this.wrapper = options.wrapper;
                this.pages = this.wrapper.children('.event');
                this.pageCount = this.pages.length;
                this.scrollTop = 0;
                this.isScroll = false;
                this._bindEvent();
            },

            _bindEvent: function(){
                var self = this;
                if (window.addEventListener) {
                    window.addEventListener('DOMMouseScroll', function(event) {self.scroll.call(self, event)}, false);
                    window.addEventListener('mousewheel', function(event) {
                        self.scroll.call(self, event);
                    }, false);
                    window.addEventListener('MozMousePixelScroll', function(event) {
                        event.preventDefault();
                    }, false);
                } else {
                    document.onmousewheel = function() {
                        self.scroll.call(self);
                    };
                }

                // change.page事件
                var topDelta = 0,
                    animateName = isMac ? 'mac' : 'pc';
                var animateFn = {
                    mac: function(scrollTop) {
                        $scrollElem.animate({
                            scrollTop: scrollTop
                        }, 1000, function() {
                            setTimeout(function() {
                                self.isScroll = false;
                            }, 500);
                        });
                    },
                    pc: function(scrollTop) {
                        $scrollElem.animate({
                            scrollTop: scrollTop
                        }, function() {
                            self.isScroll = false;
                        });
                    }
                };
                this.wrapper.on('changepage', function(event, data){
                    var $nextPage = self.pages.eq(data.nextIndex);
                    self.pages.eq(data.prevIndex).trigger('exit');
                    $nextPage.trigger('enter');
                    self.scrollTop = data.nextIndex === 0 ? 0 : $nextPage.offset().top;
                    self.scrollTop -= topDelta;
                    animateFn[animateName](self.scrollTop);
                });


                // pages 进入/退出事件
                self.pages.on('enter', function() {
                    var $this = $(this);
                    self.onEnter($this);
                });
                self.pages.on('exit', function() {
                    var $this = $(this);
                    self.onExit($this);
                });
            },
            scroll: function(event) {
                var oEvent = event || window.event;
                if (oEvent.preventDefault) {
                    oEvent.preventDefault();
                } else {
                    oEvent.returnValue = false;
                }
                if (this.isScroll){
                    return;
                }
                this.isScroll = true;
                var self = this,
                    delta = oEvent.wheelDelta ? oEvent.wheelDelta : -oEvent.detail;
                var curIndex = 0;

                if (delta < 0) {
                    curIndex = Math.min((self.curIndex + 1), self.pageCount - 1);
                } else {
                    curIndex = Math.max((self.curIndex - 1), 0);
                }
                self.setIndex(curIndex);
            },
            onEnter: function($dom) {
                $dom.addClass('animate-enter').removeClass('animate-exit');
            },
            onExit: function($dom) {
                $dom.removeClass('animate-enter').addClass('animate-exit');
            },
            setIndex: function(index) {
                var prevIndex = this.curIndex;
                this.curIndex = index;
                this.wrapper.trigger('changepage', {
                    prevIndex: prevIndex,
                    nextIndex: index
                });
            }
        };
        var pageCtrl = new PageCtrl({
            wrapper: $('.index')
        });
        /*滚动整屏效果结束*/
    }

    //滚动到指定模块加载动画
    var animateBlock={
        isVisiable:function(el,wh,st,delta){
            delta=delta||160;
            return $(el).offset().top<wh+st-delta;
        },
        animations:{
            ev2:function(wh,st){
                var $el=$("#ev_02");
                if(animateBlock.isVisiable($el,wh,st)){
                    setTimeout(function() {
     					   $('#ev_02').find(".ev2_title").animate({opacity: 1,'top':'0'},1000);
   					 },0);
   					 setTimeout(function() {
     					   $('#ev_02').find(".title2").animate({opacity: 1},1500);
   					 },0);
   					 setTimeout(function() {
     					   $('#ev_02').find(".slide_change").animate({opacity: 1,'top':'30px'},1000);
   					 },0);  					
                    delete animateBlock.animations.ev2;
                }
            },
            ev3:function(wh,st){
                var $el=$("#ev_03");
                if(animateBlock.isVisiable($el,wh,st)){
                	 setTimeout(function() {
     					   $('#ev_03').find(".ev3_title").animate({opacity: 1,'top':'0'},500);
   					 },0);
   					 setTimeout(function() {
     					   $('#ev_03').find(".title3").animate({opacity: 1,'left':'30%'},500);
   					 },500);
   					  setTimeout(function() {
     					   $('#ev_03').find(".icon_01").animate({opacity: 1},500);
   					 },500);
   					   setTimeout(function() {
     					   $('#ev_03').find(".icon_02").animate({opacity: 1},500);
   					 },1000);
   					   setTimeout(function() {
     					   $('#ev_03').find(".icon_03").animate({opacity: 1},500);
   					 },1500);
   			
                    delete animateBlock.animations.ev3;
                }
            },
            ev4:function(wh,st){
                var $el=$("#ev_04");
                if(animateBlock.isVisiable($el,wh,st)){
                	 setTimeout(function() {
     					   $('#ev_04').find(".ev4_title").animate({opacity: 1,'left':'44%'},800);
   					 },500);
   					  setTimeout(function() {
     					   $('#ev_04').find(".title4").animate({opacity: 1,'left':'37%'},800);
   					 },500);
   				 	 setTimeout(function() {
     					   $('#ev_04').find(".pic1").animate({opacity: 1,'top':'27%'},800);
   					 },500);
                    delete animateBlock.animations.ev4;
                }
            },
            ev5:function(wh,st){
                var $el=$("#ev_05");
                if(animateBlock.isVisiable($el,wh,st)){
                	
                	setTimeout(function() {
                	  		$('#ev_05').find(".GPS1").animate({opacity: 1},1000);      					   				    
   					 },0);
   					 setTimeout(function() {
                	  		$('#ev_05').find(".GPS3").animate({opacity: 1},1000);      					   				    
   					 },500);
   					 setTimeout(function() {
                	  		$('#ev_05').find(".GPS5").animate({opacity: 1},1000);      					   				    
   					 },1000);
   					  setTimeout(function() {
                	  		$('#ev_05').find(".GPS4").animate({opacity: 1},1000);      					   				    
   					 },1500);   					 
   					  setTimeout(function() {
                	  		$('#ev_05').find(".GPS6").animate({opacity: 1},1000);      					   				    
   					 },1500);
   					  setTimeout(function() {
                	  		$('#ev_05').find(".GPS2").animate({opacity: 1},1000);      					   				    
   					 },2000);
   					  setTimeout(function() {
                	  		$('#ev_05').find(".center_content").animate({opacity: 1},1000);      					   				    
   					 },2500);
                    delete animateBlock.animations.ev5;
                }
            }
            
            
        }
    };
    $(window).on("scroll",function(){
        var animations,
            name,
            winHeight=$(window).height(),
            scrollTop=$(window).scrollTop();
        animations=animateBlock.animations;
        for(name in animations){
            animations[name](winHeight,scrollTop);
        }
    });
})